#include "giteeissues.h"

#include <httpclient.h>

GiteeIssues::GiteeIssues()
{

}
const QString getTimeAgo(int timestamp);
QList<QListWidgetItem *> GiteeIssues::searching_item(const QString &text)
{
    if (m_list.isEmpty()) {
        QString uri = QString("https://gitee.com/api/v5/repos/%1/%2/issues").arg(m_owner).arg(m_repo);
        Request req(uri);
        req.addQuery("state", "open");
        req.addQuery("sort", "created");
        req.addQuery("direction", "desc");
        req.addQuery("page", "1");
        req.addQuery("per_page", "50");

        auto resp = HttpClient::instance().get(req);

        m_list = resp.toList();
    }

    QList<QListWidgetItem *> list;
    foreach (auto var, m_list) {
        auto map = var.toMap();
        map.value("id").toInt();
        auto url = map.value("url").toString();
        auto html_url = map.value("html_url").toString();
        map.value("repository_url").toString();
        auto created_at = map.value("created_at").toString();
        auto issue_state = map.value("issue_state").toString();
        map.value("comments_url").toString();
        auto title = map.value("title").toString();

        if (text.isEmpty() || title.contains(text, Qt::CaseInsensitive)) {
            QListWidgetItem *item = newItem();
            item->setText(QString("[%2] %1").arg(title).arg(getTimeAgo(QDateTime::fromString(created_at, Qt::ISODateWithMs).toTime_t())));
            item->setData(990, html_url);
            list << item;
        }
    }

    return list;
}

QString GiteeIssues::content_item(const QListWidgetItem *item)
{
    return item->data(990).toString();
}


const QString getTimeAgo(int timestamp)
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    int currentTimestamp = currentDateTime.toTime_t();

    bool isFuture = false;
    int secDiff = 0;
    if ( timestamp > currentTimestamp )//future
    {
        isFuture = true;
        secDiff = timestamp - currentTimestamp;
    }
    else if ( timestamp < currentTimestamp )//past
    {
        isFuture = false;
        secDiff = currentTimestamp - timestamp;
    }
    else//the same
    {
        return QString(QObject::tr("Just now"));
    }

    QString ret;

    if ( secDiff > 365*24*60*60 )//longer than 1 year
    {
        int years = secDiff / (365*24*60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ years in the future").arg(years);
        }
        else
        {
            ret = QObject::tr("%1+ years ago").arg(years);
        }
    }
    else if ( secDiff <= 365*24*60*60 && 30*24*60*60 < secDiff )//shorter than 1 year and longer than 1 month
    {
        int months = secDiff / (30*24*60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ months in the future").arg(months);
        }
        else
        {
            ret = QObject::tr("%1+ months ago").arg(months);
        }
    }
    else if ( secDiff <= 30*24*60*60 && 24*60*60 < secDiff )//shorter than 1 month and longer than 1 day
    {
        int days = secDiff / (24*60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ days in the future").arg(days);
        }
        else
        {
            ret = QObject::tr("%1+ days ago").arg(days);
        }
    }
    else if ( secDiff <= 24*60*60 && 60*60 < secDiff )//shorter than 1 day and longer than 1 hour
    {
        int hours = secDiff / (60*60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ hours in the future").arg(hours);
        }
        else
        {
            ret = QObject::tr("%1+ hours ago").arg(hours);
        }
    }
    else if ( secDiff <= 60*60 && 60 < secDiff )//shorter than 1 hour and longer than 1 minute
    {
        int minutes = secDiff / (60);
        if ( isFuture )
        {
            ret = QObject::tr("%1+ minutes in the future").arg(minutes);
        }
        else
        {
            ret = QObject::tr("%1+ minutes ago").arg(minutes);
        }
    }
    else if ( secDiff <= 60 )//shorter than 1 minute
    {
        if ( isFuture )
        {
            ret = QObject::tr("less than 1 minute in the future");
        }
        else
        {
            ret = QObject::tr("less than 1 minute ago");
        }
    }
    return ret;
}



// https://gitee.com/api/v5/repos/GXDE-OS/GXDE/issues?state=open&sort=created&direction=desc&page=1&per_page=20

/** ******************************************************************************************************************************************************
[
    {
        "id": 18935859,
        "url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/issues/IB9V03",
        "repository_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE",
        "labels_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/issues/IB9V03/labels",
        "comments_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/issues/IB9V03/comments",
        "html_url": "https://gitee.com/GXDE-OS/GXDE/issues/IB9V03",
        "parent_url": null,
        "number": "IB9V03",
        "parent_id": 0,
        "depth": 0,
        "state": "open",
        "title": "内测rolling chlog",
        "body": "* 同步上游更新动态壁纸\n* 推荐安装 深度计算器\n* arm内核更新到6.12.5\n* 优化 GXDE 热区在非特效模式下的使用体验\n* 文件管理器支持更换背景\n\n---\n\n已推送稳定版：\n* 修复: 全局搜索按钮模糊/提示颜色错误/自带背景\n* 修复: Garma 图标模糊 未适配高分屏\n* 修复: 无法加入内测\n* 新增: 565.77 N卡驱动 感谢 @qinyinan \n* 调整: 因武汉线路无法访问 gxde.org ，切换到 gxde.top\n* 修复: 更新器修复升级systemd失败\n* 调整: 玲珑更新到 1.7.4-1",
        "user": {
            "id": 4915358,
            "login": "shenmo7192",
            "name": "shenmo",
            "avatar_url": "https://foruda.gitee.com/avatar/1714956823131325812/4915358_shenmo7192_1714956823.png",
            "url": "https://gitee.com/api/v5/users/shenmo7192",
            "html_url": "https://gitee.com/shenmo7192",
            "remark": "",
            "followers_url": "https://gitee.com/api/v5/users/shenmo7192/followers",
            "following_url": "https://gitee.com/api/v5/users/shenmo7192/following_url{/other_user}",
            "gists_url": "https://gitee.com/api/v5/users/shenmo7192/gists{/gist_id}",
            "starred_url": "https://gitee.com/api/v5/users/shenmo7192/starred{/owner}{/repo}",
            "subscriptions_url": "https://gitee.com/api/v5/users/shenmo7192/subscriptions",
            "organizations_url": "https://gitee.com/api/v5/users/shenmo7192/orgs",
            "repos_url": "https://gitee.com/api/v5/users/shenmo7192/repos",
            "events_url": "https://gitee.com/api/v5/users/shenmo7192/events{/privacy}",
            "received_events_url": "https://gitee.com/api/v5/users/shenmo7192/received_events",
            "type": "User"
        },
        "labels": [],
        "assignee": null,
        "collaborators": [],
        "repository": {
            "id": 33307422,
            "full_name": "GXDE-OS/GXDE",
            "human_name": "GXDE OS/GXDE OS 支持中心",
            "url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE",
            "namespace": {
                "id": 13152751,
                "type": "group",
                "name": "GXDE OS",
                "path": "GXDE-OS",
                "html_url": "https://gitee.com/GXDE-OS"
            },
            "path": "GXDE",
            "name": "GXDE OS 支持中心",
            "owner": {
                "id": 7896131,
                "login": "gfdgd-xi",
                "name": "gfdgd xi",
                "avatar_url": "https://foruda.gitee.com/avatar/1712724633115947948/7896131_gfdgd-xi_1712724632.png",
                "url": "https://gitee.com/api/v5/users/gfdgd-xi",
                "html_url": "https://gitee.com/gfdgd-xi",
                "remark": "",
                "followers_url": "https://gitee.com/api/v5/users/gfdgd-xi/followers",
                "following_url": "https://gitee.com/api/v5/users/gfdgd-xi/following_url{/other_user}",
                "gists_url": "https://gitee.com/api/v5/users/gfdgd-xi/gists{/gist_id}",
                "starred_url": "https://gitee.com/api/v5/users/gfdgd-xi/starred{/owner}{/repo}",
                "subscriptions_url": "https://gitee.com/api/v5/users/gfdgd-xi/subscriptions",
                "organizations_url": "https://gitee.com/api/v5/users/gfdgd-xi/orgs",
                "repos_url": "https://gitee.com/api/v5/users/gfdgd-xi/repos",
                "events_url": "https://gitee.com/api/v5/users/gfdgd-xi/events{/privacy}",
                "received_events_url": "https://gitee.com/api/v5/users/gfdgd-xi/received_events",
                "type": "User"
            },
            "assigner": {
                "id": 7896131,
                "login": "gfdgd-xi",
                "name": "gfdgd xi",
                "avatar_url": "https://foruda.gitee.com/avatar/1712724633115947948/7896131_gfdgd-xi_1712724632.png",
                "url": "https://gitee.com/api/v5/users/gfdgd-xi",
                "html_url": "https://gitee.com/gfdgd-xi",
                "remark": "",
                "followers_url": "https://gitee.com/api/v5/users/gfdgd-xi/followers",
                "following_url": "https://gitee.com/api/v5/users/gfdgd-xi/following_url{/other_user}",
                "gists_url": "https://gitee.com/api/v5/users/gfdgd-xi/gists{/gist_id}",
                "starred_url": "https://gitee.com/api/v5/users/gfdgd-xi/starred{/owner}{/repo}",
                "subscriptions_url": "https://gitee.com/api/v5/users/gfdgd-xi/subscriptions",
                "organizations_url": "https://gitee.com/api/v5/users/gfdgd-xi/orgs",
                "repos_url": "https://gitee.com/api/v5/users/gfdgd-xi/repos",
                "events_url": "https://gitee.com/api/v5/users/gfdgd-xi/events{/privacy}",
                "received_events_url": "https://gitee.com/api/v5/users/gfdgd-xi/received_events",
                "type": "User"
            },
            "description": "GXDE OS: 稳定，优雅，轻量的Linux操作系统\nGXDE 基于 DDE15 重生构建，全称为 Gorgeous eXtended Deepin Environment ，致力于提供稳定，优雅，轻量的使用体验，减少新手痛苦，节省老手时间\nGXDE Devepoler center",
            "private": false,
            "public": true,
            "internal": false,
            "fork": false,
            "html_url": "https://gitee.com/GXDE-OS/GXDE.git",
            "ssh_url": "git@gitee.com:GXDE-OS/GXDE.git",
            "forks_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/forks",
            "keys_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/keys{/key_id}",
            "collaborators_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/collaborators{/collaborator}",
            "hooks_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/hooks",
            "branches_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/branches{/branch}",
            "tags_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/tags",
            "blobs_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/blobs{/sha}",
            "stargazers_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/stargazers",
            "contributors_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/contributors",
            "commits_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/commits{/sha}",
            "comments_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/comments{/number}",
            "issue_comment_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/issues/comments{/number}",
            "issues_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/issues{/number}",
            "pulls_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/pulls{/number}",
            "milestones_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/milestones{/number}",
            "notifications_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/notifications{?since,all,participating}",
            "labels_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/labels{/name}",
            "releases_url": "https://gitee.com/api/v5/repos/GXDE-OS/GXDE/releases{/id}",
            "recommend": true,
            "gvp": false,
            "homepage": "https://www.gxde.org",
            "language": "C++",
            "forks_count": 2,
            "stargazers_count": 74,
            "watchers_count": 10,
            "default_branch": "master",
            "open_issues_count": 47,
            "has_issues": true,
            "has_wiki": true,
            "issue_comment": false,
            "can_comment": true,
            "pull_requests_enabled": true,
            "has_page": false,
            "license": "GPL-3.0",
            "outsourced": false,
            "project_creator": "gfdgd-xi",
            "members": [
                "zty199",
                "gfdgd-xi",
                "shenmo7192"
            ],
            "pushed_at": "2024-12-26T22:56:43+08:00",
            "created_at": "2024-02-15T08:43:43+08:00",
            "updated_at": "2024-12-30T10:13:08+08:00",
            "parent": null,
            "paas": null,
            "assignees_number": 1,
            "testers_number": 1,
            "assignee": [
                {
                    "id": 7896131,
                    "login": "gfdgd-xi",
                    "name": "gfdgd xi",
                    "avatar_url": "https://foruda.gitee.com/avatar/1712724633115947948/7896131_gfdgd-xi_1712724632.png",
                    "url": "https://gitee.com/api/v5/users/gfdgd-xi",
                    "html_url": "https://gitee.com/gfdgd-xi",
                    "remark": "",
                    "followers_url": "https://gitee.com/api/v5/users/gfdgd-xi/followers",
                    "following_url": "https://gitee.com/api/v5/users/gfdgd-xi/following_url{/other_user}",
                    "gists_url": "https://gitee.com/api/v5/users/gfdgd-xi/gists{/gist_id}",
                    "starred_url": "https://gitee.com/api/v5/users/gfdgd-xi/starred{/owner}{/repo}",
                    "subscriptions_url": "https://gitee.com/api/v5/users/gfdgd-xi/subscriptions",
                    "organizations_url": "https://gitee.com/api/v5/users/gfdgd-xi/orgs",
                    "repos_url": "https://gitee.com/api/v5/users/gfdgd-xi/repos",
                    "events_url": "https://gitee.com/api/v5/users/gfdgd-xi/events{/privacy}",
                    "received_events_url": "https://gitee.com/api/v5/users/gfdgd-xi/received_events",
                    "type": "User"
                }
            ],
            "testers": [
                {
                    "id": 7896131,
                    "login": "gfdgd-xi",
                    "name": "gfdgd xi",
                    "avatar_url": "https://foruda.gitee.com/avatar/1712724633115947948/7896131_gfdgd-xi_1712724632.png",
                    "url": "https://gitee.com/api/v5/users/gfdgd-xi",
                    "html_url": "https://gitee.com/gfdgd-xi",
                    "remark": "",
                    "followers_url": "https://gitee.com/api/v5/users/gfdgd-xi/followers",
                    "following_url": "https://gitee.com/api/v5/users/gfdgd-xi/following_url{/other_user}",
                    "gists_url": "https://gitee.com/api/v5/users/gfdgd-xi/gists{/gist_id}",
                    "starred_url": "https://gitee.com/api/v5/users/gfdgd-xi/starred{/owner}{/repo}",
                    "subscriptions_url": "https://gitee.com/api/v5/users/gfdgd-xi/subscriptions",
                    "organizations_url": "https://gitee.com/api/v5/users/gfdgd-xi/orgs",
                    "repos_url": "https://gitee.com/api/v5/users/gfdgd-xi/repos",
                    "events_url": "https://gitee.com/api/v5/users/gfdgd-xi/events{/privacy}",
                    "received_events_url": "https://gitee.com/api/v5/users/gfdgd-xi/received_events",
                    "type": "User"
                }
            ],
            "status": "开始",
            "programs": [],
            "enterprise": null,
            "project_labels": [],
            "issue_template_source": "project"
        },
        "milestone": null,
        "created_at": "2024-12-09T04:34:08+08:00",
        "updated_at": "2024-12-26T13:31:22+08:00",
        "plan_started_at": null,
        "deadline": null,
        "finished_at": null,
        "scheduled_time": 0,
        "comments": 5,
        "priority": 0,
        "issue_type": "任务",
        "program": null,
        "security_hole": false,
        "issue_state": "待办的",
        "branch": null,
        "issue_type_detail": {
            "id": 1,
            "title": "任务",
            "template": null,
            "ident": "task",
            "color": "#0086D6",
            "is_system": true,
            "created_at": "2017-09-01T03:09:12+08:00",
            "updated_at": "2017-09-01T03:09:12+08:00"
        },
        "issue_state_detail": {
            "id": 1,
            "title": "待办的",
            "color": "#8c92a4",
            "icon": "icon-task-state-21",
            "command": null,
            "serial": 1,
            "created_at": "2017-09-01T03:09:13+08:00",
            "updated_at": "2017-09-01T03:09:13+08:00"
        }
    }
]
********************************************************************************************************************************************************/
